// 博客相关的方法

const {execSQL} = require('../db/mysql');

// 获取博客列表数据
const getBlogsList = (author, keyword) => {
    let sql = `select * from blogs where 1=1 `;

    if(author){
        sql += `and author = '${author}' `;
    }
    if(keyword){
        sql += `and title like '%${keyword}%' `;
    }
    return execSQL(sql);
}

// 获取博客详情数据
const getBlogDetail = (id) => {
    const sql = `select * from blogs where id = '${id}'`;
    return execSQL(sql).then(rows => {
        return rows[0];
    });
}

// 创建新的博客
const createNewBlog = (blogData = {}) => {
    const title = blogData.title;
    const content = blogData.content;
    const author = blogData.author;
    const createAt = Date.now();
    const sql = `
        insert into blogs (title, content, author, createdAt) values ('${title}', '${content}', '${author}', ${createAt});
    `;
    return execSQL(sql).then(insertedResult => {
        //console.log('insertedResult', insertedResult);
        return {
            id: insertedResult.insertId
        }
    });
}

// 更新博客
const updateBlog = (id, blogData = {}) => {
    const title = blogData.title;
    const content = blogData.content;
    const sql = `update blogs set title='${title}', content='${content}' where id=${id};`;
    return execSQL(sql).then(updateResult => {
        console.log('updateResult', updateResult);
        if(updateResult.affectedRows > 0){
            return true;
        }
        return false;
    });
}

// 删除博客
// 如果每个人只能删除自己的博客，则再接收一个参数“作者”
const deleteBlog = (id, author) => {
    const sql = `delete from blogs where id = ${id} and author = '${author}'`;
    return execSQL(sql).then(deleteResult => {
        console.log('deleteResult', deleteResult);
        if(deleteResult.affectedRows > 0){
            return true;
        }
        return false;
    });
}

module.exports = {
    getBlogsList, 
    getBlogDetail,
    createNewBlog,
    updateBlog,
    deleteBlog
}